package com.haizhi.upload.component;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;


public class FileUpLoadUtil {
	private String[] type;
	private String path;
	private long size;
	private UpdateLoadFileStrategy strategy; 
		
	public FileUpLoadUtil(UpdateLoadFileStrategy strategy) {
			type = strategy.type().split("\\.");
			path = strategy.path();
			size = strategy.size()*1024*1024;	
	}

	/**
	 * 
	 * @param item
	 * @return 返回-1,表示类型出现问题,返回-2,表示尺寸过大,文件名表示存入成功,0表示未知错误
	 */
//	public String doUpload(FileItem item, String contentPath) {
//		// 
//		String fileName = item.getName();// 获取完整名字
//		String fileUploadName = fileName
//				.substring(fileName.lastIndexOf("\\") + 1);
//		String typeName = fileUploadName.substring(fileUploadName
//				.lastIndexOf(".") + 1);
//		if (item.getSize() >= size) {
//			return "-2";
//		}
//
//		int count = 0;
//		for (String aType : type) {
//			if (aType.toLowerCase().equals(typeName.toLowerCase())) {
//				count = count + 1;
//			}
//		}
//
//		if (count == 1) {
//			// 往文件名里面加上时间戳
//
//			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
//			int random = (int) (Math.random()*1000000);
//			String timeStamp = sdf.format(new Date());
//			fileUploadName = timeStamp +random+  fileUploadName.substring(fileUploadName
//					.lastIndexOf(".") );
//			try {
//				File file = new File(contentPath + path);
//				if (!file.exists()) {
//					file.mkdirs();
//				}
//
//				item.write(new File(contentPath + path,fileUploadName));
//				//如果是一个图片就进行转化处理
//				if(!this.isMusic){
//					File changeFile = new File(contentPath+changePricturePath);
//					if(!changeFile.exists()){
//						changeFile.mkdirs();
//					}
//					ImageChange ic = new ImageChange(contentPath + path+"\\"+fileUploadName,contentPath + changePricturePath+"\\"+fileUploadName);
//					System.out.print(contentPath);
//					ic.flex(Integer.valueOf(changePrictureWidth),Integer.valueOf(changePrictureHeight));
//					return "/"+changePricturePath+"/"+fileUploadName;
//				}else{
//					return "/"+path+"/"+fileUploadName;
//				}
//				
//
//			} catch (Exception e) {
//				// 
//				e.printStackTrace();
//	
//				return "0";
//			}
//
//		} else {
//			
//			return "-1";
//		}
//
//	}
	/**
	 * 返回-1,表示类型出现问题,返回-2,表示尺寸过大,文件名表示存入成功,0表示未知错误
	 * jiangjintai
	 * 2016年3月23日
	 * @param image
	 * @param realPath
	 * @return
	 */
	public String doUpload(MultipartFile image, String realPath) {
		String fileName = image.getOriginalFilename();// 获取完整名字
		System.out.println("fileName:"+fileName);
		String fileUploadName = fileName
				.substring(fileName.lastIndexOf("\\") + 1);
		System.out.println("fileUploadName:"+fileUploadName);
		String typeName = fileUploadName.substring(fileUploadName
				.lastIndexOf(".") + 1);
		if (image.getSize() >= size) {
			return "-2";
		}

		int count = 0;
		if("*".equals(type[0]))//代表全部类型
			count=1;
		for (String aType : type) {
			if (aType.toLowerCase().equals(typeName.toLowerCase())) {
				count = count + 1;
			}
		}
		System.out.println("count:"+count);
		if (count == 1) {
			// 往文件名里面加上时间戳
			SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
			int random = (int) (Math.random()*1000000);
			String timeStamp = sdf.format(new Date());
			fileUploadName = timeStamp +random+  fileUploadName.substring(fileUploadName
					.lastIndexOf(".") );
			try {
				File file = new File(realPath + path);
				if (!file.exists()) {
					file.mkdirs();
				}
				//这里用这个方式进行存储
				FileUtils.writeByteArrayToFile(new File(realPath + path,fileUploadName), image.getBytes());
				//如果是一个图片就进行转化处理
				if(strategy instanceof Compressable){
					Compressable compressable = (Compressable) this.strategy;
					File changeFile = new File(realPath+compressable.changedPath());
					if(!changeFile.exists()){
						changeFile.mkdirs();
					}
					File uploadPrictureFile = new File(realPath + path,fileUploadName);
					File changePrictureFile = new File(realPath + compressable.changedPath(),fileUploadName);
					ImageChange ic = new ImageChange(uploadPrictureFile,changePrictureFile);
					ic.flex(Integer.valueOf(compressable.pictureWidth()),Integer.valueOf(compressable.pictureLength()));
					System.out.print(realPath);
					return "/"+compressable.changedPath()+"/"+fileUploadName;
				}else{
					return "/"+path+"/"+fileUploadName;
				}
			} catch (Exception e) {

				e.printStackTrace();
	
				return "0";
			}

		} else {
			
			return "-1";
		}
	}
}
